from db.operationDb.admin import getIdByAccount
from db.operationDb.adminShop import insertAdminShop
from db.operationDb.shopEleInfo import geteleIdByShopAccount, getAllEleShop
from db.operationDb.shopMeituanInfo import getmtIdByShopAccount, getAllMtShop
from spider import MTSpider, ELESpider
from web.service.overData.sql_db import getShopInfoByShopId, getAllShopInfoByShopId
from db.bidding import AdminShop

def changeNewCustomPrice(shopId, shopType, nowPrice):
    """
    更改新科出价的api
    :param shopId:
    :param shopType:
    :param nowPrice:
    :return:
    """
    try:
        #获取该店铺的信息
        shopInfo = getShopInfoByShopId(shopId, shopType)
        #美团店铺使用当前ip修改新客出价
        spider = None
        if shopType == 1:
            spider = MTSpider(shopInfo.get("cookies"))
        else:
            spider = ELESpider(shopInfo.get("cookies"))
        res = spider.setCustomerPrice(nowPrice, shopInfo.get("ip"))
        return 1
    except:
        return 0

def getNewCustomPrice(shopType, shopId):
    """
    获取新客出价
    :param shopType:
    :param shopId:
    :return:
    """
    try:
        shopInfo = getShopInfoByShopId(shopId, shopType)
        if shopType == 1:
            spider = MTSpider(shopInfo.get("cookies"))
        else:
            spider = ELESpider(shopInfo.get("cookies"))
        res = spider.getCustomerPrice(shopInfo.get("ip")).get("newCustomer")
        res = round((res / 100), 2)
    except:
        res = -1
    return res

def getallTime(shopId, shopType, busyTime):
    """
    获取切分好的时间段
    :param shopId:
    :param shopType:
    :param busyTime:
    :return:
    """
    shipTime = None
    shopInfo = getAllShopInfoByShopId(shopId, shopType)
    if not shopInfo:
            return 0 #没有获取到店铺的信息
    # try:
    #     shipTime = eval(shopInfo.shipTime)
    # except:
    #     return 0
    idleTime = []
    shipTime = shopInfo.shipTime.split(",")
    specialtime = [] #type字段是区分闲忙时，1是忙时，0是闲时
    busyTime.sort()
    for i in range(len(busyTime)):
        res = busyTime[i].split("-")
        if res[0] != "00:00" and i == 0:
            idleTime.append("00:00-" + res[0])
        if i + 1 == len(busyTime) and res[1] != '24:00':
            idleTime.append(res[1] + "-24:00")
        else:
            if i + 1 < len(busyTime) and res[1] != busyTime[i + 1].split("-")[0]:
                idleTime.append(res[1] + "-" + busyTime[i + 1].split("-")[0])
    for bztime in busyTime:
        s = bztime.split("-")[0]
        e = bztime.split("-")[1]
        specialtime.append(
            {'s': s, 'e': e, 'type': 1})
    for idltime in idleTime:
        s = idltime.split("-")[0]
        e = idltime.split("-")[1]
        specialtime.append(
            {'s': s, 'e': e, 'type': 0})
    #按照开始时间的先后顺序将所有的时间段进行排序
    specialtimed = sorted(specialtime, key=lambda x : x['s'])
    if specialtime:
        specialtimed[0]['s'] = shipTime[0].split("-")[0]
        specialtimed[-1]['e'] = shipTime[-1].split("-")[1]
    last_specialtime = []
    for sp in specialtimed:
        if sp['s'] < sp['e']:
            last_specialtime.append(sp)
        else:
            continue
    return last_specialtime

def bindAdminShopApi(account, shops):
    """
    绑定账号与店铺的service层
    :param account:
    :param shops:
    :return:
    """
    #1、通过account查询admin表获取admin_id
    adminId = getIdByAccount(account)
    if adminId == -1:
        return -1
    #2、通过shops中的shopId查询满足条件的店铺的id，需要判断是一个，还是多个
    try:
        shops = eval(shops)
    except:
        shops = shops
    adminShop = []
    for shop in shops:
        shopAccount = shop.get("account")
        shopType = int(shop.get("shopType"))
        if shopType == 1:
            shopInfoId = getmtIdByShopAccount(shopAccount)
        else:
            shopInfoId = geteleIdByShopAccount(shopAccount)
        if shopInfoId == -1:
            continue
        adminShop.append(AdminShop(
            admin_id=adminId,
            shop_id=shopInfoId,
            shop_type = shopType
        ))
    if len(adminShop) == 0:
        return -3
    #3、将admin_id与shop的id插入到AdminShop表中
    res = insertAdminShop(adminShop)
    if res == -1:
        return -2

def getAllShopInfo():
    mtShop = getAllMtShop()
    eleShop = getAllEleShop()
    return {"mt":mtShop, "ele":eleShop}